package com.cyufeng.blog.controller;

import com.cyufeng.blog.dto.ArticleDto;
import com.cyufeng.blog.entity.Article;
import com.cyufeng.blog.service.ArticleSerivce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 文章
 * @author Cyufeng
 * Data:2019.1.05
 */
@CrossOrigin
@RequestMapping("/blog/article")
@RestController
public class ArticleController {
    @Autowired
    ArticleSerivce articleSerivce;
    /**
     *保存文章
     * @param articleDto
     * @return
     */
    @PostMapping("/saveArticle")
    public Map<String,Object> saveMoodTest(@RequestBody ArticleDto articleDto) {
        Map<String,Object> r = articleSerivce.saveArticle(articleDto);
        return r;
    }

    /**
     * 分页查询所有文章
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/getArticle")
    public Map<String, Object> getArticle(@RequestParam(value = "page") Integer page, @RequestParam(value = "size") Integer size, @RequestParam(value = "sort") Integer sort){
        return articleSerivce.getArticle(page,size,sort);
    }

    /**
     * 根据id查找文章
     * @param articleId
     * @return
     */
    @GetMapping("/getOneArticle")
    public ArticleDto getOneArticle(@RequestParam(value = "articleId") int articleId){
        return articleSerivce.getOneArticle(articleId);
    }

    /**
     * 分页查询所有工具
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/getTool")
    public Map<String, Object> getTool(@RequestParam(value = "page") Integer page, @RequestParam(value = "size") Integer size){
        return articleSerivce.getTool(page,size);
    }

    /**
     * 根据id查找工具
     * @param toolId
     * @return
     */
    @GetMapping("/getOneTool")
    public ArticleDto getOneTool(@RequestParam(value = "toolId") int toolId){
        return articleSerivce.getOneTool(toolId);
    }

    /**
     * 改变是否启用
     *
     * @return
     */
    @GetMapping("/setIsEnable")
    public String setIsEnable(@RequestParam("isEnable") Boolean isEnable, @RequestParam("id") int id) {
        return articleSerivce.setIsEnbale(isEnable, id);
    }


    /**
     * 分页查询所有文章-用于后台管理
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/getAllArticle")
    public Map<String, Object> getAllArticle(@RequestParam(value = "page") Integer page, @RequestParam(value = "size") Integer size, @RequestParam(value = "sort") Integer sort){
        return articleSerivce.getAllArticle(page,size,sort);
    }

    /**
     * 删除用户
     *
     * @return
     */
    @GetMapping("/deleteArticle")
    public String deleteArticle(@RequestParam("id") int id) {

        return articleSerivce.deleteArticle(id);
    }

    /**
     * 分页查询所有工具--用于后台操作
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/getAllTool")
    public Map<String, Object> getAllTool(@RequestParam(value = "page") Integer page, @RequestParam(value = "size") Integer size){
        return articleSerivce.getAllTool(page,size);
    }
}
